package com.b1502.store2.activity;

import android.content.Intent;

import android.os.Bundle;
import android.util.Log;



import android.view.View;

import android.widget.Button;
import android.widget.TextView;

import com.b1502.store2.R;


import com.b1502.store2.adapter.ConfirmAdapter;
import com.b1502.store2.model.bean.OrderBean;
import com.b1502.store2.network.PostParams;
import com.b1502.store2.network.StoreParams;
import com.b1502.store2.util.UrlUtil;

import com.b1502.store2.widget.MyListView;
import com.google.gson.Gson;


import org.mym.plog.PLog;
import org.xutils.common.Callback;
import org.xutils.x;


import java.util.ArrayList;

import java.util.List;

/**
 * 订单确认页面
 */
public
class
ConfirmOrderActivity extends BaseActivity {


    private TextView shouhuo;
    private TextView sp;
    List<OrderBean.OrderDetailsBean> orderDetailsBeanList = new ArrayList<OrderBean.OrderDetailsBean>();
    private TextView zj;
    private MyListView myListView;


    private String orderId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirm_order);
        //跳转传值
        initView();
        orderId = getIntent().getStringExtra("orderId");
        getOrder("orderId", orderId);
        //getAccount();
        //confirmOrder(orderId, "", "");

    }

    public void getremoveItems() {

        int num = 0;
        for (int i = 0; i < orderDetailsBeanList.size(); i++) {
            num++;
        }

        String[] productIds = new String[num];
        int count = 0;
        for (int i = 0; i < orderDetailsBeanList.size(); i++) {

            productIds[count] = orderDetailsBeanList.get(i).ProductId;
            count++;
        }
        PLog.e(productIds[0] + "----长度");
        removeItems(productIds);


    }

    public void initView() {

        shouhuo = (TextView) findViewById(R.id.shouhuo);
        sp = (TextView) findViewById(R.id.sp);
        zj = (TextView) findViewById(R.id.zj);
        myListView = (MyListView) findViewById(R.id.confirm_list);
        Button button= (Button) findViewById(R.id.tijiao);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                confirmOrder(orderId,"","");

            }
        });

    }

    /**
     * 获取订单
     */
    public void getOrder(String param, String orderId) {
        StoreParams params = new StoreParams(UrlUtil.GetOrder, param, orderId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onSuccess(String result) {
                orderDetailsBeanList.clear();
                OrderBean orderBean = new Gson().fromJson(result, OrderBean.class);
                zj.setText("总计: ￥" + orderBean.Sum);
                sp.setText("+ ￥" + orderBean.Sum);
                shouhuo.setText(orderBean.ReceiptAddress);
                orderDetailsBeanList.addAll(orderBean.OrderDetails);
                ConfirmAdapter confirmAdapter = new ConfirmAdapter(ConfirmOrderActivity.this, orderDetailsBeanList);
                myListView.setAdapter(confirmAdapter);
                //getremoveItems();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    /**
     * 获取账户余额
     */
    public void getAccount() {
        StoreParams params = new StoreParams(UrlUtil.GetAccount);
        x.http().get(params, new Callback.CommonCallback<String>() {

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onSuccess(String result) {
                //Log.d(TAG, result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    /**
     * 确认订单
     *
     * @param orderId 订单ID
     * @param remark
     * @param invoice
     */
    public void confirmOrder(final String orderId, String remark, String invoice) {
        StoreParams params = new StoreParams(UrlUtil.ConfirmOrder);
        params.setAsJsonContent(true);
        PostParams postParams = new PostParams(orderId, remark, invoice);
        params.setBodyContent(new Gson().toJson(postParams));

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onSuccess(String result) {
                //TODO 支付页面
                // PLog.e(result);
                Intent intent=new Intent(ConfirmOrderActivity.this,OrderDetailActivity.class);
                intent.putExtra("orderId",orderId);
                startActivity(intent);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    /**
     * 移除购物车
     *
     * @param productIds 产品ID集合
     */
    public void removeItems(String[] productIds) {
        StoreParams params = new StoreParams(UrlUtil.RemoveItems);
        params.setAsJsonContent(true);
        PostParams postParams = new PostParams(productIds);
        params.setBodyContent(new Gson().toJson(postParams));

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onSuccess(String result) {
                // TODO 更新购物车数量
                //Log.d(TAG, result);
                PLog.e(result + "移除购物车");
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

}
